<template>

</template>

<script setup name="HazardAlarm">
import {ElNotification} from "element-plus";
import useUserStore from "@/store/modules/user.js";
import {getHazardAlarm} from "@/api/business/hazard.js";
import {setDeltaId} from "@/utils/auth.js";

const emits = defineEmits(['click'])
const userStore = useUserStore()

const checkAlarm = async () => {
    let payload = undefined
    if (userStore.deltaId) payload = {deltaId: userStore.deltaId}
    const {data} = await getHazardAlarm(payload)
    userStore.deltaId = data.deltaId
    setDeltaId(data.deltaId) // keep into cookie at the same time.
    if (!data.delayedCount && !data.majorCount) return

    const title = payload?.deltaId ? '隐患更新警告' : '隐患统计警告'
    let msgHtml = '<div class="text-content cursor-pointer">'
    if (data.majorCount) msgHtml += '<div>发现' + data.majorCount + '条隐患即将到达限期</div>'
    if (data.delayedCount) msgHtml += '<div>发现' + data.delayedCount + '条隐患延期</div>'
    msgHtml += '<a class="link-type mt5">去查看</a>'
    msgHtml += '</div>'

    const notifier = ElNotification({
        title: title,
        type: 'warning',
        duration: 0,
        dangerouslyUseHTMLString: true,
        message: msgHtml,
        onClick: () => {
            notifier.close()
            emits('click')
        }
    })
}

onMounted(() => {
    const interval = 5 * 60 * 1000 // 5分钟
    setInterval(() => checkAlarm(), interval)
    checkAlarm() // call immediately once
})
</script>

<style scoped>

</style>